home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0093_Basm routines.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  12KB  |  244 lines

  1. procedure CopySubStr( Str1: string; start, nrchars: byte; var Str2: string );
  2. assembler;
  3.   { copy part of Str1 (beginning at start for nrchars) to Str2
  4.     if start > length of Str1, Str2 will contain a empty string.
  5.     if nrchars specifies more characters than remain starting at the
  6.     start position, Str2 will contain just that remainder of Str1. }
  7. asm
  8.         { setup }
  9.         LDS   SI,Str1      { load in DS:SI pointer to str1 }
  10.         CLD                { string operations forward     }
  11.         LES   DI,Str2      { load in ES:DI pointer to str2 }
  12.         MOV   AH,[SI]      { length str1 --> AH            }
  13.         AND   AH,AH        { length str1 = 0?              }
  14.         JE    @null        { yes, empty string in Str2     }
  15.         MOV   BL,[start]   { starting position --> BL      }
  16.         CMP   AH,BL        { start > length str1?          }
  17.         JB    @null        { yes, empty string in Str2     }
  18.  
  19.         { start + nrchars - 1 > length str1?               }
  20.         MOV   AL,[nrchars] { nrchars --> AL                }
  21.         MOV   DH,AL        { nrchars --> DH                }
  22.         ADD   DH,BL        { add start                     }
  23.         DEC   DH
  24.         CMP   AH,DH        { nrchars > rest of str1?       }
  25.         JB    @rest        { yes, copy rest of str1        }
  26.         JMP   @copy
  27. @null:
  28.         MOV   AL,0         { return a empty string         }
  29.         JMP   @done
  30. @rest:
  31.         SUB   AH,BL        { length str1 - start           }
  32.         INC   AH
  33.         MOV   AL,AH
  34. @copy:
  35.         MOV   CL,AL        { how many chars to copy        }
  36.         XOR   CH,CH        { clear CH                      }
  37.         XOR   BH,BH        { clear BH                      }
  38.         ADD   SI,BX        { starting position             }
  39.         MOV   DX,DI        { save pointer to str2          }
  40.         INC   DI
  41.         REP   MOVSB        { copy part str1 to str2        }
  42.         MOV   DI,DX        { restore pointer to str2       }
  43. @done:
  44.         MOV   [DI],AL      { overwrite length byte of str2 }
  45. @exit:
  46. end  { CopySubStr };
  47.  
  48.  
  49. procedure StrCopy( var Str1, Str2: string ); assembler;
  50.   { copy str1 to str2 }
  51. asm
  52.         LDS   SI,Str1    { load in DS:SI pointer to str1 }
  53.         CLD              { string operations forward     }
  54.         LES   DI,Str2    { load in ES:DI pointer to str2 }
  55.         XOR   CH,CH      { clear CH                      }
  56.         MOV   CL,[SI]    { length str1 --> CX            }
  57.         INC   CX         { include length byte           }
  58.         REP   MOVSB      { copy str1 to str2             }
  59. @exit:
  60. end  { StrCopy };
  61.  
  62. function StrPos( var str1, str2: string ): byte; assembler;
  63.   { returns position of the first occurrence of str1 in str2 }
  64.   { return value in AX }
  65.   { str1 - string to search for }
  66.   { str2 - string to search in  }
  67. asm
  68.         CLD              { string operations forward                 }
  69.         LES   DI,Str2    { load in ES:DI pointer to str2             }
  70.         XOR   CH,CH      { clear CH                                  }
  71.         MOV   CL,[DI]    { length str2 --> CX                        }
  72.         AND   CX,CX      { length str2 = 0?                          }
  73.         JZ    @Negatief  { length str2 = 0, nothing to search in     }
  74.         INC   DI         { make DI point to the 1st char of str2     }
  75.         LDS   SI,Str1    { load in DS:SI pointer to str1             }
  76.         LODSB            { load in AL length str1                    }
  77.         AND   AL,AL      { length str1 = 0?                          }
  78.         JZ    @Negatief  { length str1 = 0, nothing to search for    }
  79.         MOV   AH,AL      { length str1 --> AH                        }
  80.         DEC   AH         { 1st char need not be compared again       }
  81.         LODSB            { load in AL 1st character of str1          }
  82. @Start:
  83.   REPNE SCASB            { scan for next occurrence 1st char in str2 }
  84.         JNE   @Negatief  { no success                                }
  85.         CMP   CL,AH      { length str1 > # chars left in str2 ?      }
  86.         JB    @Negatief  { yes, str1 not in str2                     }
  87.         MOV   DX,SI      { pointer to 2nd char in str1 --> DX        }
  88.         MOV   BX,CX      { number of chars in str2 to go --> BX      }
  89.         MOV   CL,AH      { length str1 --> CL                        }
  90.         REPE  CMPSB      { compare until characters don't match      }
  91.         JE    @Positief  { full match                                }
  92.         SUB   SI,DX      { current SI - prev. SI = # of chars moved  }
  93.         SUB   DI,SI      { reconstruct DI                            }
  94.         MOV   SI,DX      { restore pointer to 2nd char in str1       }
  95.         MOV   CX,BX      { number of chars in str2 to go --> BX      }
  96.         JMP   @Start     { scan for next occurrence 1st char in str2 }
  97. @Negatief:
  98.         XOR   AX,AX      { str1 is not in str, result 0              }
  99.         JMP   @Exit
  100. @Positief:
  101.         XOR   AH,AH      { clear AH                                  }
  102.         LES   DI,Str2    { load in ES:DI pointer to str2             }
  103.         MOV   AL,[DI]    { length str2 --> AX                        }
  104.         SUB   AX,BX      { start position of str1 in str2            }
  105. @Exit:                   { we are finished. }
  106. end  { StrPos };
  107.  
  108. procedure Trim( var Str: string ); assembler;
  109.   { remove leading and trailing white space from str }
  110. asm
  111.         { setup }
  112.         LDS   SI,Str     { load in DS:SI pointer to Str       }
  113.         MOV   AX,DS      { Set ES to same segment as DS       }
  114.         MOV   ES,AX      { Set ES to same segment as DS       }
  115.         MOV   AL,[SI]    { length Str --> AL                  }
  116.         AND   AL,AL      { length Str = 0?                    }
  117.         JZ    @exit      { yes, nothing to do                 }
  118.         MOV   DI,SI      { pointer to Str --> DI              }
  119.         MOV   AH,AL      { length Str --> AH                  }
  120.  
  121.         { remove trailing white space }
  122.         XOR   CH,CH      { clear CH                           }
  123.         MOV   CL,AH      { length Str --> CX                  }
  124.         ADD   SI,CX      { start with last character          }
  125. @start1:
  126.         MOV   AL,[SI]    { character  --> AL                  }
  127.         CMP   AL,20H     { no white space                     }
  128.         JA    @stop1     { last non-blank character found     }
  129.         DEC   SI         { count down SI                      }
  130.         DEC   CL         { count down CX                      }
  131.         AND   CL,CL      { more characters left?              }
  132.         JZ    @stop1     { no, done                           }
  133.         JMP   @start1    { try again                          }
  134. @stop1:
  135.         AND   CL,CL      { length Str = 0?                    }
  136.         JZ    @done      { string is empty, done              }
  137.  
  138.         { look for leading white space }
  139.         MOV   SI,DI      { pointer to Str --> SI              }
  140. @start2:
  141.         INC   SI         { next character                     }
  142.         MOV   AL,[SI]    { character  --> AL                  }
  143.         CMP   AL,20H     { no white space                     }
  144.         JA    @stop2     { first non-blank character found    }
  145.         DEC   CL         { count down                         }
  146.         AND   CL,CL      { more characters left?              }
  147.         JZ    @stop2     { no, done                           }
  148.         JMP   @start2    { try again                          }
  149. @stop2:
  150.         MOV   DX,SI      { difference between SI and DI gives }
  151.         SUB   DX,DI      { position first non-blank character }
  152.         CMP   DX,1       { first character non-blank?         }
  153.         JE    @done      { yes, done                          }
  154.  
  155.         { remove leading white space }
  156.         CLD              { string operations forward          }
  157.         MOV   BX,CX      { save length Str                    }
  158.         MOV   DX,DI      { save pointer to Str                }
  159.         INC   DI         { don't overwrite length byte of Str }
  160.         REP   MOVSB      { move remaining part of Str         }
  161.         MOV   DI,DX      { restore pointer to Str             }
  162.         MOV   CX,BX      { restore length Str                 }
  163. @done:
  164.         MOV   [DI],CL    { overwrite length byte of Str       }
  165. @exit:
  166. end  { Trim };
  167.  
  168.  
  169. procedure RTrim( var Str: string ); assembler;
  170.   { remove trailing white space from str }
  171. asm
  172.         { setup }
  173.         LDS   SI,Str     { load in DS:SI pointer to Str      }
  174.         MOV   AL,[SI]    { length Str --> AL                 }
  175.         AND   AL,AL      { length Str = 0?                   }
  176.         JZ    @exit      { yes, exit                         }
  177.         MOV   DI,SI      { pointer to Str --> DI             }
  178.         MOV   AH,AL      { length Str --> AH                 }
  179.  
  180.         { remove trailing space }
  181.         STD              { SeT Direction flag --> backwards  }
  182.         XOR   CH,CH      { clear CH                          }
  183.         MOV   CL,AH      { length Str --> CX                 }
  184.         ADD   SI,CX      { start with last character         }
  185. @start:
  186.         MOV   AL,[SI]    { character  --> AL                 }
  187.         CMP   AL,20H     { no white space                     }
  188.         JA    @stop      { last non-blank character found    }
  189.         DEC   SI         { count down                        }
  190.         DEC   CL         { count down                        }
  191.         AND   CL,CL      { more characters left?             }
  192.         JZ    @stop      { no, done                          }
  193.         JMP   @start     { try again                         }
  194. @stop:
  195.         MOV   [DI],CL    { overwrite length byte of Str      }
  196. @exit:
  197. end  { RTrim };
  198.  
  199.  
  200. procedure LTrim( var Str: string ); assembler;
  201.   { remove leading white space from str }
  202. asm
  203.         { setup }
  204.         LDS   SI,Str     { load in DS:SI pointer to Str       }
  205.         MOV   AL,[SI]    { length Str --> AL                  }
  206.         AND   AL,AL      { length Str = 0?                    }
  207.         JZ    @exit      { yes, nothing to do                 }
  208.         MOV   DI,SI      { pointer to Str --> DI              }
  209.         XOR   CH,CH      { clear CH                           }
  210.         MOV   CL,AL      { length Str --> CX                  }
  211.  
  212.         { look for leading white space }
  213. @start:
  214.         INC   SI         { next character                     }
  215.         MOV   AL,[SI]    { character  --> AL                  }
  216.         CMP   AL,20H     { no white space                     }
  217.         JA    @stop      { first non-blank character found    }
  218.         DEC   CL         { count down                         }
  219.         AND   CL,CL      { more characters left?              }
  220.         JZ    @nullstr   { no, done                           }
  221.         JMP   @start     { try again                          }
  222. @nullstr:
  223.         MOV   CL,0       { null string                        }
  224.         JMP   @done      { we're done                         }
  225. @stop:
  226.         MOV   DX,SI      { difference between SI and DI gives }
  227.         SUB   DX,DI      { position first non-blank character }
  228.         CMP   DX,1       { first character non-blank?         }
  229.         JE    @exit      { yes, exit                          }
  230.  
  231.         { remove leading white space }
  232.         CLD              { string operations forward          }
  233.         MOV   DX,CX      { save length Str                    }
  234.         MOV   BX,DI      { save pointer to Str                }
  235.         INC   DI         { don't overwrite length byte of Str }
  236.         REP   MOVSB      { move remaining part of Str         }
  237.         MOV   DI,BX      { restore pointer to Str             }
  238.         MOV   CX,DX      { restore length Str                 }
  239. @done:
  240.         MOV   [DI],CL    { overwrite length byte of Str       }
  241. @exit:
  242. end  { LTrim };
  243.  
  244.